/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.ncn.background;

import java.util.concurrent.TimeUnit;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class TrackingService extends Service {

    private static final float MIN_LOCATION_UPDATE_DIST = 10;
    private static final long MIN_LOCATION_UPDATE_TIME = TimeUnit.SECONDS.toMillis(30);
    
    private LocationListener locListener;
    private PhoneStateListener psListener;
    private WifiSignalTracker wifiTracker;

    private DatabaseCleaner databaseCleaner;
    private LocationManager locManager;
    private TelephonyManager teleManager;
    private WifiManager wifiManager;
        
    @Override
    public void onCreate() {
        getSystemServices();
        setupBackgroundProcesses();
    }
    
    private void getSystemServices() {
        locListener = new TrackingLocationListener();
        psListener = new TrackingPhoneStateListener();
        
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        teleManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }
    
    /**
     * Initializes data collection procedures. Starts collections location, cell signal, and wifi
     * signal information. Each listener / data collector stores said data in the application
     * SQLite database.
     */
    private void setupBackgroundProcesses() {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_LOCATION_UPDATE_TIME,
                MIN_LOCATION_UPDATE_DIST, locListener);
        teleManager.listen(psListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        
        wifiTracker = new WifiSignalTracker(wifiManager);
        wifiTracker.start();
        
        databaseCleaner = new DatabaseCleaner();
        databaseCleaner.start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
